/* 
 *  Created By: Mike Stevens
 *  Project URL: http://www.codeplex.com/sitesecuritymgmt
 *  License: GNU GPL v2
 *  Copyright (C) 2008 Mike Stevens
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using SiteSecurityManagement.Data;
using System.Data;

namespace SiteSecurityManagement.Service
{
    [Serializable]
    class BulkEditSecurityDataService
    {
        #region Members
        private DataTableWrapper m_userDataTable;

        //column names in data table
        //added to data table in this order
        private const int IDCOLNUM = 0;
        private const int USERCOLNUM = 1;
        private const int USERTYPECOLNUM = 2;
        private const int USERNAMECOLNUM = 3;
        private const int ROLECOLNUM = 4;

        /// <summary>
        /// ID Column
        /// </summary>
        private string m_idCol = "colId";
        /// <summary>
        /// User/Group Display Name
        /// </summary>
        private string m_usersCol = "colUsersGroups";
        /// <summary>
        /// User/Group Type
        /// </summary>
        private string m_usersTypeCol = "colType";
        /// <summary>
        /// User/Group Account Name
        /// </summary>
        private string m_usersNameCol = "colUserName";
        /// <summary>
        /// Specified permissions for User/Group
        /// </summary>
        private string m_usersPermsCol = "colPermissions";

        #endregion

        #region Constructor/Destructor
        public BulkEditSecurityDataService(string tableName)
        {
            m_userDataTable = new DataTableWrapper(tableName);
        }
        #endregion

        #region Properties
        public int IDColumnNumber
        {
            get
            {
                return IDCOLNUM;
            }
        }

        public int UserDisplayNameColumnNumber
        {
            get
            {
                return USERCOLNUM;
            }
        }

        public int UserTypeColumnNumber
        {
            get
            {
                return USERTYPECOLNUM;
            }
        }

        public int UserAccountNameColumnNumber
        {
            get
            {
                return USERNAMECOLNUM;
            }
        }

        public int UserPermissionsColumnNumber
        {
            get
            {
                return ROLECOLNUM;
            }
        }

        public string IDColumnName
        {
            get
            {
                return m_idCol;
            }
        }
        public string UserDisplayNameColumnName
        {
            get
            {
                return m_usersCol;
            }
        }

        public string UserTypeColumnName
        {
            get
            {
                return m_usersTypeCol;
            }
        }

        public string UserAccountNameColumnName
        {
            get
            {
                return m_usersNameCol;
            }
        }

        public string UserPermissionsColumnName
        {
            get 
            {
                return m_usersPermsCol;
            }
        }

        public DataView GetDataSource
        {
            get
            {
                return m_userDataTable.BindableDataSource;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets the items.
        /// </summary>
        /// <returns>a List of object arrays</returns>
        public List<object[]> GetItems()
        {
            return m_userDataTable.GetItems;
        }

        /// <summary>
        /// Builds the data table.
        /// </summary>
        /// <param name="dict">The dictionary of data.</param>
        public void BuildDataTable(Dictionary<string, string>[] dict)
        {
            BuildDataTable();

            for (int i = 0; i < dict.Length; i++)
            {
                m_userDataTable.AddDataRow(dict[i]);
            }
        }

        /// <summary>
        /// Deletes the row.
        /// </summary>
        /// <param name="id">The id.</param>
        public void DeleteRow(int id)
        {
            m_userDataTable.DeleteRow(id);
        }

        /// <summary>
        /// Gets the row count.
        /// </summary>
        /// <returns></returns>
        public int GetRowCount()
        {
            return m_userDataTable.GetRowCount();
        }

        /// <summary>
        /// Builds the data table.
        /// </summary>
        private void BuildDataTable()
        {
            //Build table to mimic user.aspx list
            //ID column
            m_userDataTable.AddDataColumn(m_idCol, "System.Int32", true);

            //Users/Groups column
            m_userDataTable.AddDataColumn(m_usersCol, "System.String", false);

            //Type column
            m_userDataTable.AddDataColumn(m_usersTypeCol, "System.String", false);

            //User Name column
            m_userDataTable.AddDataColumn(m_usersNameCol, "System.String", false);

            //Permissions column
            m_userDataTable.AddDataColumn(m_usersPermsCol, "System.String", false);
        }
        #endregion
    }
}
